import time

from selenium.common import NoAlertPresentException
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class BasePage:
    _BASE_URL = "http://8.137.19.140:9090/blog_login.html"

    def __init__(self, base_driver=None):
        if base_driver:
            self.driver = base_driver
        else:
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(10)
            self.driver.maximize_window()

        if not self.driver.current_url.startswith("http"):
            self.driver.get(self._BASE_URL)



    def do_find(self, by, locator=None):
        if locator:
            return self.driver.find_element(by, locator)
        else:
            return self.driver.find_element(*by)

    def do_finds(self, by, locator=None):
        if locator:
            return self.driver.find_elements(by, locator)
        else:
            return self.driver.find_elements(*by)

    def do_send_keys(self, by, value, locator=None):
        ele = self.do_find(by, locator)
        ele.clear()
        ele.send_keys(value)

    def do_click(self, by, locator=None):
        self.do_find(by, locator).click()

    def do_clear(self, by, locator=None):
        self.do_find(by, locator).clear()

    def do_get_text(self, by, locator=None):
        text = self.do_find(by, locator).text
        return text

    def do_quit(self):
        self.driver.quit()

    def wait_visibility_of_element_located(self, locator, timeout=10):
        return WebDriverWait(self.driver, timeout).until(
            expected_conditions.visibility_of_element_located(locator)
        )

    def wait_element_to_be_clickable(self, locator, timeout=10):
        return WebDriverWait(self.driver, timeout).until(
            expected_conditions.element_to_be_clickable(locator)
        )

    def mac_clear_send(self, by, locator=None, text=None):
        # 定位编辑器容器
        editor = self.do_find(by, locator)
        # 等待编辑器加载（延长等待时间，确保元素可交互）
        time.sleep(3)
        # 初始化动作链
        actions = ActionChains(self.driver)
        # 聚焦编辑器
        actions.click(editor)
        # Mac 全选（Command+A）
        actions.key_down(Keys.COMMAND).send_keys("a").key_up(Keys.COMMAND)
        # 删除内容
        actions.send_keys(Keys.DELETE)
        # 输入新内容
        actions.send_keys(text)
        # 执行所有操作
        actions.perform()
        # 等待输入完成
        time.sleep(1)

    def win_clear_send(self, by, locator=None, text=None):
        # 定位编辑器容器
        editor = self.do_find(by, locator)
        # 等待编辑器加载（延长等待时间，确保元素可交互）
        time.sleep(1)
        # 初始化动作链
        actions = ActionChains(self.driver)
        # 聚焦编辑器
        actions.click(editor)
        # Mac 全选（Command+A）
        actions.key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL)
        # 删除内容
        actions.send_keys(Keys.DELETE)
        # 输入新内容
        actions.send_keys(text)
        # 执行所有操作
        actions.perform()
        # 等待输入完成
        time.sleep(1)

    def alert_accept(self):
        try:
            alert = self.alert_wait()
            alert.accept()
            print("成功处理弹窗")
        except NoAlertPresentException:
            print("未检测到弹窗，继续执行")
        except Exception as e:
            print("处理弹窗时发生未知错误：", e)

    def alert_wait(self):
        return WebDriverWait(self.driver, 10).until(EC.alert_is_present())
